package com.cn.yang.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.production.ProductModel;
import com.cn.yang.service.ProductService;
import com.cn.yang.service.impl.ProductServiceImpl;
import com.cn.yang.util.UpLoad;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/addProduct")
public class AddProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        UpLoad upLoad = new UpLoad();
        String string = upLoad.imgUpload(req);
        String aa[] = string.split("@@",-1);
        ProductModel model = new ProductModel();
        model.setClassify(Integer.parseInt(aa[0]));
        model.setName(aa[1]);
        model.setImg(aa[2]);
        model.setContent(aa[3]);
        model.setDiliver(aa[4]);
        model.setAverageprice(aa[5]);
        model.setPrice(aa[6]);
        model.setQuality(aa[7]);
        model.setPayment(aa[8]);
        model.setInformation(aa[9]);

        ProductService service = new ProductServiceImpl();
        int result = service.addProduct(model);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
}
